import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_module/common/utils/object_to_any.dart';
import 'package:flutter_module/common/utils/size_fit_utils.dart';
import 'package:flutter_module/common/widget/empty_view/empty_view.dart';
import 'package:flutter_module/common/widget/skeleton/content_page_skeleton.dart';
import 'package:flutter_module/core/flutter_bridge.dart';
import 'package:flutter_module/style/colors.dart';

import 'base_list.dart';

export 'package:flutter_module/common/utils/extensions.dart';

abstract class BaseStatelessWidget extends StatelessWidget {
  const BaseStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    SizeFit.initialize(context);
    return createView(context);
  }

  Widget createView(BuildContext context);
}

abstract class BaseStateFulWidget extends StatefulWidget {
  const BaseStateFulWidget({Key? key}) : super(key: key);

  @override
  BaseState createState() => buildState();

  BaseState buildState();
}

abstract class BaseState<T extends StatefulWidget> extends State<T> {
  bool isDispose = false;
  bool isInit = false;
  bool isLoading = false; // 加载中,注意这个加载中是骨架屏
  bool isEmpty = false; // 显示空布局

  @override
  Widget build(BuildContext context) {
    SizeFit.initialize(context);
    return createView(context);
  }

  Widget createView(BuildContext context);

  @override
  void initState() {
    isInit = true;
    super.initState();
  }

  @override
  void dispose() {
    isDispose = true;
    super.dispose();
  }
}

mixin BasePage<Page extends BaseStateFulWidget> on BaseState<Page> {
  EasyRefreshController esRefreshController = EasyRefreshController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Widget showView;

    if (isLoading) {
      showView = createSkeleton();
    } else if (isEmpty) {
      showView = createEmptyView();
    } else {
      // 修改加载顺序
      Widget child = createView(context);

      if (this is ListBehavior) {
        ListBehavior behavior = this as ListBehavior;
        child = createRefresh(child) ??
            behavior.createEasyRefresh(child, controller: esRefreshController);
      }
      showView = child;
    }

    return Scaffold(
      appBar: createAppBar(),
      body: createBody(showView, context),
    );
  }

  Widget createBody(Widget child, BuildContext context) {
    return WillPopScope(
      child: Container(
        color: ThemeColors.grayFBFAFA,
        child: child,
      ),
      onWillPop: () async {
        return await onBack();
      },
    );
  }

  Widget createView(BuildContext context);

  /// appbar api

  createAppBar() {
    final titleWidget = Text(
      appBarTitle,
      style: TextStyle(
        color: ThemeColors.text333,
        fontSize: 17.0,
        fontWeight: FontWeight.w500,
      ),
    );
    if (_isIOS) {
      return CupertinoNavigationBar(
        leading: leading(),
        backgroundColor: Colors.white,
        trailing: _iosTrailingWidget(),
        middle: titleWidget,
      );
    } else {
      return AppBar(
        backgroundColor: Colors.white,
        elevation: 1.0,
        leading: leading(),
        centerTitle: true,
        title: titleWidget,
        actions: getActions(),
      );
    }
  }

  Widget? _iosTrailingWidget() {
    final actionList = getActions() ?? [];
    if (actionList.isNotEmpty) {
      return actionList.first;
    }
    return null;
  }

  String get appBarTitle;

  bool get _isIOS => defaultTargetPlatform == TargetPlatform.iOS;

  leading() {
    if (_isIOS) {
      return CupertinoButton(
        child: Icon(
          Icons.arrow_back_ios,
          color: ThemeColors.text333,
          size: 20.0,
        ),
        alignment: Alignment.centerLeft,
        onPressed: onBack,
        padding: EdgeInsets.zero,
      );
    }
    return IconButton(
      icon: Icon(Icons.arrow_back_ios),
      onPressed: onBack,
      iconSize: 20.0,
      padding: EdgeInsets.only(left: 16.0),
      alignment: Alignment.centerLeft,
      color: ThemeColors.text333,
    );
  }

  onBack() async {
    Navigator.canPop(context)
        ? Navigator.pop(context)
        : FlutterPlatformBridge.getInstance().onBack(null);
    return false;
  }

  List<Widget>? getActions() => null;

  /// end appbar api

  /// 空布局 api

  /// 可复写重新加载按钮点击
  reloadPress() {}

  /// end 空布局 api

  // 创建骨架屏
  createSkeleton() {
    return ContentPageSkeleton();
  }

  createEmptyView() {
    return EmptyView(message: Text('暂无数据'));
  }

  createRefresh(Widget child) => null;
}
